package com.groupbuying.redpacket.controller;
import com.groupbuying.redpacket.service.DelayTimerService;
import com.groupbuying.redpacket.service.GetRedPacketService;
import com.groupbuying.redpacket.service.HandleRedPacketTaskService;
import com.groupbuying.redpacket.utils.apiresult.ApiResult;
import com.groupbuying.redpacket.utils.apiresult.ApiResultCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author Vincentxin
 * @Date 2019/11/12
 */
@RestController
@RequestMapping("/base")
public class BaseController {
    Logger logger = LoggerFactory.getLogger(BaseController.class);

    @Autowired
    private HandleRedPacketTaskService handleRedPacketTaskService;

    @Autowired
    private GetRedPacketService getRedPacketService;

    @Autowired
    private DelayTimerService delayTimerService;


    /**
     * @api {get} /base/handleTaskClose 手动关闭任务
     * @apiDescription 手动关闭任务
     * @apiGroup redPacket
     * @apiHeader {String} Authorization 用户授权token
     * @apiSuccess {JSON} ApiResult "{"result":"success"}" 或者 "{"result":"error"}"
     * @author srlg
     * @date 2019-12-12
     * @apiVersion 1.0.0
     * @tar 红包后台
     */
    @GetMapping("/handleTaskClose")
    public ApiResult handleTaskClose(String taskId) {
        if (StringUtils.isEmpty(taskId)){
            logger.error("手动关闭任务,错误提示：任务id不能为空");
            return ApiResult.failure("任务关闭错误，任务id为空");
        }

        String result = "{\"result\":\"success\"}";

        //关闭任务
        boolean executeResult = handleRedPacketTaskService.taskFinish(taskId);

        //未执行成功
        if (!executeResult) {
            result = "{\"result\":\"error\"}";
        }

        return ApiResult.success(ApiResultCode.SUCCESS,result);
    }
    /**
     * @api {get} /base/delayStartTask 延时开始任务
     * @apiDescription 延时开始任务
     * @apiGroup redPacket
     * @apiHeader {String} Authorization 用户授权token
     * @apiSuccess {JSON} void 无
     * @author srlg
     * @date 2019-11-25
     * @apiVersion 1.0.0
     * @tar 红包后台
     */
    @GetMapping("/delayStartTask")
    public ApiResult delayStartTask(String taskId) {
        if (StringUtils.isEmpty(taskId)){
            logger.error("延时启动,错误提示：任务id不能为空");
            return ApiResult.failure("启动错误，任务id为空");
        }

       return delayTimerService.timerControl(taskId);
    }
    /**
     * //用户摇红包接口
     *
     * @Author Vincentxin
     * @Date 2019/11/26 17:26
     * @param
     * @return com.groupbuying.redpacket.utils.apiresult.ApiResult
     **/
    @GetMapping("/customerGetRedPacket")
    public ApiResult customerGetRedPacket(){
        return  getRedPacketService.getRedPacket();
    }

}
